package com.newlink.building.activities

import android.view.View
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.common_base.constant.NL_Key
import com.newlink.building.common_base.constant.Base_RouterPath
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.common_base.utils.getSpValue
import com.newlink.building.common_base.utils.putSpValue
import com.newlink.building.R
import com.newlink.building.common_base.utils.logDebug
import com.newlink.building.constant.Module_Phone_Constant
import com.newlink.building.databinding.DialActivityBinding
import com.newlink.building.manager.Module_Phone_VoipController

/**
 * Created by xiaodong on 2022/7/29.
 */
@Route(path = Base_RouterPath.PATH_DIAL)
class Module_Phone_DialActivityNL : NL_BaseActivity() {

    private var domain: String = ""
    private var realm: String = ""

    private lateinit var mBinding: DialActivityBinding
    override fun attachLayoutRes(): View {
        mBinding = DialActivityBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
    }

    override fun initView() {
        domain = getSpValue(Module_Phone_Constant.DOMAIN_ADDRESS, "172.16.1.2")
        realm = getSpValue(Module_Phone_Constant.REALM_ADDRESS, "")

        var userName = ""
        var serverAddress = ""
        if (Module_Phone_VoipController.get().coreContext.core.defaultAccount != null) {
            userName =
                Module_Phone_VoipController.get().coreContext.core.defaultAccount!!.findAuthInfo()?.username!!
            serverAddress =
                Module_Phone_VoipController.get().coreContext.core.defaultAccount!!.findAuthInfo()?.domain!!
        }
        when (Module_Phone_VoipController.get().isOnLine) {
            true -> {
                mBinding.registerState.setTextColor(ContextCompat.getColor(this, R.color.white))
                mBinding.registerState.text = "设备在线"
                mBinding.registerUsername.setTextColor(ContextCompat.getColor(this, R.color.white))
                mBinding.registerServer.setTextColor(ContextCompat.getColor(this, R.color.white))
                mBinding.registerUsername.text = "用户: $userName"
                mBinding.registerServer.text = "Sip服务: $serverAddress"
            }

            else -> {
                mBinding.registerState.setTextColor(ContextCompat.getColor(this, R.color.grey400))
                mBinding.registerState.text = "设备离线"
                mBinding.registerUsername.text = ""
                mBinding.registerServer.text = ""
            }
        }
        mBinding.call.isEnabled = Module_Phone_VoipController.get().isOnLine
        mBinding.video.isEnabled = Module_Phone_VoipController.get().isOnLine
        mBinding.call.setOnClickListener {
            outgoingCall()
        }
        mBinding.video.setOnClickListener {
            videoCall()
        }

        var account = getSpValue(NL_Key.SIP_USERNAME, "") as String
        mBinding.etCallNumber.setText(account)
        mBinding.btnSave.setOnClickListener {
            val text = mBinding.etCallNumber.text.toString().trim()
            if (!text.contains("sip:")) {
                account = "sip:$text@$domain"
            } else {
                account = text
            }
            putSpValue(NL_Key.SIP_USERNAME, account)
            mBinding.etCallNumber.setText(account)
            showToast("保存成功！")
        }

    }
//    private var domain = sp("account_info").getString(Constant.DOMAIN_ADDRESS,"172.16.13.150")

    private fun videoCall() {
        var remoteSipUri = mBinding.remoteAddress.text.toString()
        val remoteIp = if (realm.isEmpty()) domain else realm
        if (!remoteSipUri.contains("sip")) {
            remoteSipUri = "sip:$remoteSipUri@$remoteIp"
        }
        logDebug<Module_Phone_DialActivityNL>("remoteSipUri $remoteSipUri")
        Module_Phone_VoipController.get().makeVideoCallWholeAdress(remoteSipUri)
    }

    private fun outgoingCall() {
        var remoteSipUri = mBinding.remoteAddress.text.toString()
        val remoteIp = if (realm.isEmpty()) domain else realm
        if (!remoteSipUri.contains("sip")) {
            remoteSipUri = "sip:$remoteSipUri@$remoteIp"
        }
        logDebug<Module_Phone_DialActivityNL>("remoteSipUri $remoteSipUri")
        Module_Phone_VoipController.get().makeAudioCallWholeAdress(remoteSipUri)
    }
}
